#include <iostream>
#include <sstream>
#include <time.h>
#include <stdio.h>

#include <gui_defs.hpp>
#include <auto360_tools.hpp>

using namespace std;

#define HGUI_DEBUG

#ifdef HGUI_DEBUG
struct CallBackPara {
	const char* winName;
	Mat *img;
	const char* nodeName;
	const char* fileName;
};
static void on_mouse(int event, int x, int y, int flags, void* ustc) {
	static int cnt = 0;
	static Point pts[2];

	struct CallBackPara *cbp = (struct CallBackPara *) ustc;

	if (event == CV_EVENT_LBUTTONDOWN) {
		Point pt = Point(x, y);
//		circle(*cbp->img, pt, 1, Scalar(0, 0, 255), CV_FILLED, CV_AA, 0);
//		imshow(cbp->winName, *cbp->img);

		pts[cnt % 2] = pt;
		if (++cnt == 2) {
			Rect rect(pts[0], pts[1]);
			Mat btnData;

			(*cbp->img)(rect).copyTo(btnData);
			FileStorage fs(cbp->fileName, FileStorage::WRITE | FileStorage::READ);
			fs << cbp->nodeName << btnData;

			fs.release();
			cnt = 0;
		}
	}
}
#endif

void Atuo360Tools::img2icon() {
#ifdef HGUI_DEBUG

	stringstream path;
	String picDir = "/home/zj/MyDisk/workspace/auto360/data/XZT1/20160321-";
	path << picDir << "1.jpg";
	Mat img = imread(path.str(), IMREAD_COLOR);

	struct CallBackPara cbp;
	cbp.winName = "atuo360_tools";
	cbp.img = &img;
	cbp.nodeName = "icon_0";
	cbp.fileName = "icons";

	namedWindow("atuo360_tools", 1);
	setMouseCallback("atuo360_tools", on_mouse, (void*) &cbp);
	imshow("atuo360_tools", img);
	waitKey(0);

	int fsNum = 100, n = 1;
	while (n <= fsNum) {
		FileStorage fs("icons", FileStorage::READ);
		Mat mt;
		fs["icon_0"] >> mt;
		imshow("atuo360_tools", mt);
		unsigned char key = waitKey(0);
		if (key == KEY_ESC) {
			fs.release();
			break;
		}
	}

	destroyWindow("atuo360_tools");

#endif
}

